package logic

import (
	"context"
	"errors"
	"video-guard/models/mysql"

	"video-guard/videorpc/internal/svc"
	"video-guard/videorpc/videorpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type CollectionUpdateLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewCollectionUpdateLogic(ctx context.Context, svcCtx *svc.ServiceContext) *CollectionUpdateLogic {
	return &CollectionUpdateLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *CollectionUpdateLogic) CollectionUpdate(in *videorpc.CollectionUpdateRequest) (*videorpc.CollectionUpdateResponse, error) {
	// todo: add your logic here and delete this line

	var collection mysql.CollectionInfo

	err := collection.FirstCollectionById(mysql.DB, in.CollectionId)
	if err != nil {
		return nil, errors.New("该收藏不存在")
	}

	collection.CollectStatus = 1
	err = collection.CollectionUpdate(mysql.DB, in.CollectionId)
	if err != nil {
		return nil, errors.New("取消收藏失败")
	}

	return &videorpc.CollectionUpdateResponse{
		CollectionId: int64(collection.ID),
	}, nil
}
